package com.example.demo;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;


@Service
public class DataService {

    @PostConstruct
    @Scheduled(cron = "0 0 1 * * *")
    public void updateData() throws IOException {
        RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

        String uriTemplate = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        URI uri = UriComponentsBuilder.fromUriString(uriTemplate).build(42);

        RequestEntity<Void> requestEntity = RequestEntity.get(uri)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36")
                .build();

        ResponseEntity<Resource> response = template.exchange(requestEntity, Resource.class);


        Resource body = response.getBody();

        Path filepath = Path.of("./src/main/resources/static/data.csv");
        if (!Files.exists(filepath)) {
            Files.createFile(filepath);
        }
        Files.copy(body.getInputStream(), filepath, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("FILE UPDATE....");
    }

    public List<RegionStats> getList() {
        List<RegionStats> list = new java.util.ArrayList<>();

        File d = new File("./src/main/resources/static/data.csv");

        try {
            final Reader reader = new InputStreamReader(new FileInputStream(d), "UTF-8");

            final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());

            for (final CSVRecord record : parser) {
                list.add(new RegionStats(
                        record.get("Province/State"),
                        record.get("Country/Region"),
                        record.get("Lat"),
                        record.get("Long")
                ));

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {


        }

        return list;

    }

    public RegionStats searchItem(String name) {
        List<RegionStats> list = getList();
        for (RegionStats e : list) {
            if (e.getCountry().equals(name) || e.getState().equals(name)) {
                return e;
            }
        }

        return new RegionStats();
    }


}
